package com.Drools.CIPS.util;
import java.io.*;
import java.util.*;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XmlToMap {
    public static void main(String[] args) throws Exception {
        Map<String, Object> map = parseXml("D:\\Win10Driver\\OneDrive\\桌面\\案例\\pacs.008.xml");
//        Map<String, Object> map = convertXmlToMap("D:\\Win10Driver\\OneDrive\\桌面\\111.xml");
        System.out.println(map);
    }

    private static String fileToString(String path) {
        StringBuilder sb = new StringBuilder("");
        try(BufferedReader br = new BufferedReader(new FileReader(path));) {
            char[] cbuf = new char[1024];
            int len;
            while ((len = br.read(cbuf)) != -1){
                sb.append(cbuf,0,len);
                //由于汉字等某些字符  一个字符占不等的 字节 所以有汉字的用字符流好
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }


    public static Map<String, Object> parseXml(String path)  {

        Map<String, Object> map = new LinkedHashMap<>();
        try {
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(new ByteArrayInputStream(fileToString(path).getBytes("UTF-8")));//xml串第一行不能有空格，否则报错
            Element root = document.getRootElement();//得到xml文档根节点元素，即最上层的"<xml>"
            elementTomap(root, map);
        } catch (UnsupportedEncodingException | DocumentException e) {
            e.printStackTrace();
        }
        return map;
    }

    @SuppressWarnings("unchecked")
    public static Map<String, Object> elementTomap(Element outele, Map<String, Object> outmap) {
        List<Element> list = outele.elements();
        int size = list.size();

        if (size == 0) {
            outmap.put(outele.getName(), outele.getTextTrim());

            List<Attribute> attributes = outele.attributes();//查询 xml的尖括号内数据 如币种
            for (Attribute attribute : attributes){
                outmap.put(outele.getName() + attribute.getName(), attribute.getValue());
            }
        } else {
            Map<String, Object> innerMap = new LinkedHashMap<>();
            List<String> innerList = new ArrayList<>();
            String k = "";
            int i = 1;

            for (Element ele1 : list) {
                String eleName = ele1.getName();

                String value = ele1.getText();
                Object obj = innerMap.get(eleName);
                if (obj == null) {
                    elementTomap(ele1, innerMap);
                } else {
                    if (obj instanceof Map) {
                        List<Map<String, Object>> list1 = new ArrayList<>();
                        list1.add((Map<String, Object>) innerMap.remove(eleName));
                        elementTomap(ele1, innerMap);
                        list1.add((Map<String, Object>) innerMap.remove(eleName));
                        innerMap.put(eleName, list1);
                    } else if (obj instanceof String) {
                        innerMap.put(eleName, value);
                        i++;
                        k = eleName;
                        innerList.add(value);
                    }else {
                        elementTomap(ele1, innerMap);
                        Map<String, Object> listValue = (Map<String, Object>) innerMap.get(eleName);
                        ((List<Map<String, Object>>) obj).add(listValue);
                        innerMap.put(eleName, obj);
                    }

                }
            }
            if(i > 1){
                innerMap.put(k,innerList);
            }
            outmap.put(outele.getName(), innerMap);

        }
        return outmap;
    }
}


